package cn.blunce.base;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import cn.blunce.model.DaoMaster;
import cn.blunce.model.DaoMaster.DevOpenHelper;
import cn.blunce.model.DaoSession;
import cn.blunce.model.Job;
import cn.blunce.model.JobDao;
import cn.blunce.model.Person;
import cn.blunce.model.PersonDao;

/**
 * Created by Blunce on 2015/11/8 0008.
 */
public class AppDB {
    private static DevOpenHelper sHelper;
    private static SQLiteDatabase sDatabase;
    private static DaoMaster sDaoMaster;
    private static DaoSession sDaoSession;
    private static AppDB sAppDB = null;

    // ****************************数据操作****************************************
    private static PersonDao sPersonDao;
    private static JobDao sJobDao;

    private AppDB(Context context) {
        sHelper = new DaoMaster.DevOpenHelper(context, "App-DB", null);
        sDatabase = sHelper.getWritableDatabase();
        sDaoMaster = new DaoMaster(sDatabase);
        sDaoSession = sDaoMaster.newSession();

        sPersonDao = sDaoSession.getPersonDao();
        sJobDao = sDaoSession.getJobDao();
    }

    public static synchronized AppDB getInstance(Context context) {
        if (sAppDB == null) {
            sAppDB = new AppDB(context);
        }
        return sAppDB;
    }

    public static boolean add(Object object) {
        boolean tag = true;
        if ("cn.blunce.model.Person".equals(object
                .getClass().getName())) {
            sPersonDao.insertOrReplace((Person) object);
        } else if ("cn.blunce.model.Job"
                .equals(object.getClass().getName())) {
            sJobDao.insertOrReplace((Job) object);
        } else {
            tag = false;
        }
        return tag;
    }

    public static PersonDao getsPersonDao() {
        return sPersonDao;
    }

    public static JobDao getsJobDao() {
        return sJobDao;
    }

}
